package com.fau.fm.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang.time.DateFormatUtils;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.NotBlank;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

import com.fau.fm.util.SystemConfig;

/**
 * @desc 员工工资表
 * @author liy
 * @datetime 2014年11月16日,下午4:19:51
 */
@Entity
@Table(name = "medical_staff_salary")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MedicalStaffSalary implements Serializable, JSONAware {

	private static final long serialVersionUID = -5454414368998770069L;
	
	private String id;
	
	@NotNull(message = "基本工资id不能为空！")
	private MedicalStaffBasic staffBasic;	// 基本工资
	private BigDecimal effectSalary;		// 绩效工资
	private BigDecimal leaveDeduct;			// 请假扣款
	
	@NotNull(message = "实发工资不能为空！")
	private BigDecimal practicalSalary;		// 实发工资
	private Date calcStime;					// 计算工资开始时间
	private Date calcEtime;					// 计算结束时间
	
	@NotBlank(message = "医院id不能为空！")
	private String hospitalId;				// 医院id
	
	private String uid;						// 员工id
	private String startTime;				// 开始时间
	private String endTime;					// 结束时间
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "effect_salary", length = 9)
	public BigDecimal getEffectSalary() {
		return effectSalary;
	}

	public void setEffectSalary(BigDecimal effectSalary) {
		this.effectSalary = effectSalary;
	}

	@Column(name = "leave_deduct", length = 9)
	public BigDecimal getLeaveDeduct() {
		return leaveDeduct;
	}

	public void setLeaveDeduct(BigDecimal leaveDeduct) {
		this.leaveDeduct = leaveDeduct;
	}

	@Column(name = "practical_salary", length = 9)
	public BigDecimal getPracticalSalary() {
		return practicalSalary;
	}

	public void setPracticalSalary(BigDecimal practicalSalary) {
		this.practicalSalary = practicalSalary;
	}

	@Column(name = "calc_stime")
	public Date getCalcStime() {
		return calcStime;
	}

	public void setCalcStime(Date calcStime) {
		this.calcStime = calcStime;
	}

	@Column(name = "calc_etime")
	public Date getCalcEtime() {
		return calcEtime;
	}

	public void setCalcEtime(Date calcEtime) {
		this.calcEtime = calcEtime;
	}

	@Column(name = "hospital_id", length = 32)
	public String getHospitalId() {
		return hospitalId;
	}

	public void setHospitalId(String hospitalId) {
		this.hospitalId = hospitalId;
	}

	@Transient
	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}
	
	@ManyToOne
	@JoinColumn(name = "staff_basic")
	public MedicalStaffBasic getStaffBasic() {
		return staffBasic;
	}

	public void setStaffBasic(MedicalStaffBasic staffBasic) {
		this.staffBasic = staffBasic;
	}

	@Transient
	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	@Transient
	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject object = new JSONObject();
		object.put("id", id);
		object.put("name", staffBasic.getUser().getName());
		if(SystemConfig.CM_SYSTEM_TYPE.equals(staffBasic.getDeptType())){
			object.put("dept", "话务中心");
		}
		if(SystemConfig.OM_SYSTEM_TYPE.equals(staffBasic.getDeptType())){
			object.put("dept", "订单中心");
		}
		
		if(Hibernate.isInitialized(staffBasic.getUser().getGroup())) {
			object.put("platformName", staffBasic.getUser().getGroup().getPlatform().getName());
			object.put("groupName", staffBasic.getUser().getGroup().getName());
		}
		if(SystemConfig.GROUP_LEADER.equals(staffBasic.getIsLeader())) {
			object.put("positionName", "组长");
		} else if(SystemConfig.GROUP_EMPLOYEE.equals(staffBasic.getIsLeader())) {
			object.put("positionName", "组员");
		}
		object.put("basic", staffBasic.getBasic());
		object.put("effectSalary", effectSalary);
		object.put("leaveDeduct", leaveDeduct);
		object.put("practicalSalary", practicalSalary);
		object.put("hospitalId", hospitalId);
		object.put("calcStime", DateFormatUtils.format(calcStime, "yyyy-MM-dd HH:mm:ss"));
		object.put("calcEtime", DateFormatUtils.format(calcEtime, "yyyy-MM-dd HH:mm:ss"));
		object.put("startTime", startTime);
		object.put("endTime", endTime);
		return object.toJSONString();
	}
	
}
